import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:myapp/generated/app_strings.dart';
import '../models/community_info.dart';

///小区详情页面
class CommunityDetailPage extends StatelessWidget {
  final CommunityInfo community;

  const CommunityDetailPage({
    Key? key,
    required this.community,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print('详情页面构建: 纬度=${community.latitude}, 经度=${community.longitude}');
    return Scaffold(
      appBar: AppBar(
        title: Text(AppStrings.of(context).communityDetail),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          _buildInfoCard(
            context,
            title: AppStrings.of(context).basicInfo,
            children: [
              _buildInfoRow(
                context,
                label: AppStrings.of(context).communityName,
                value: community.name,
                onTap: () {
                  Clipboard.setData(ClipboardData(text: community.name));
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text(AppStrings.of(context).communityNameCopied)),
                  );
                },
              ),
              _buildInfoRow(
                context,
                label: AppStrings.of(context).communityAddress,
                value: community.address,
                onTap: () {
                  Clipboard.setData(ClipboardData(text: community.address));
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text(AppStrings.of(context).communityAddressCopied)),
                  );
                },
              ),
              if (community.latitude != null && community.longitude != null)
                Padding(
                  padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
                  child: Container(
                    width: double.infinity,
                    padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                    decoration: BoxDecoration(
                      color: Colors.grey[100],
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Row(
                      children: [
                        const Icon(Icons.location_on, size: 16, color: Colors.grey),
                        const SizedBox(width: 8),
                        Expanded(
                          child: Text(
                            '${AppStrings.of(context).latitude}: ${community.latitude!.toStringAsFixed(6)}, ${AppStrings.of(context).longitude}: ${community.longitude!.toStringAsFixed(6)}',
                            style: const TextStyle(
                              fontSize: 12,
                              color: Colors.grey,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              if (community.notes?.isNotEmpty ?? false)
                _buildInfoRow(
                  context,
                  label: AppStrings.of(context).notes,
                  value: community.notes ?? '',
                ),
            ],
          ),
          const SizedBox(height: 16),
          _buildInfoCard(
            context,
            title: AppStrings.of(context).facilities,
            children: [
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).allowDeliveryBike,
                value: community.allowDelivery,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).hasDoorControl,
                value: community.hasDoorControl,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).hasElevator,
                value: community.hasElevator,
              ),
              if (community.hasElevator)
                _buildSwitchRow(
                  context,
                  label: AppStrings.of(context).elevatorNeedCard,
                  value: community.elevatorNeedCard,
                ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).yuyaoJiangnanSouth,
                value: community.isInYuyaoJiangnanSouth,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).ningciEastNorth,
                value: community.isInNingciEastNorth,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).hongtang,
                value: community.isInHongtang,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).nearMixc,
                value: community.isNearMixc,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).easyDelivery,
                value: community.isEasyDelivery,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).hardDelivery,
                value: community.isHardDelivery,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).isBusiness,
                value: community.isBusiness,
              ),
              _buildSwitchRow(
                context,
                label: AppStrings.of(context).hasDeliveryCabinet,
                value: community.hasDeliveryCabinet,
              ),
            ],
          ),
          if (community.buildingPasswords.isNotEmpty) ...[
            const SizedBox(height: 16),
            _buildInfoCard(
              context,
              title: AppStrings.of(context).buildingPasswords,
              children: community.buildingPasswords.map((password) {
                return _buildInfoRow(
                  context,
                  label: password.buildingNumber,
                  value: password.password,
                  onTap: () {
                    Clipboard.setData(ClipboardData(text: password.password));
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text(AppStrings.of(context).passwordCopied)),
                    );
                  },
                  subtitle: password.notes,
                );
              }).toList(),
            ),
          ],
          if (community.notes?.isNotEmpty ?? false) ...[
            const SizedBox(height: 16),
            Text(
              AppStrings.of(context).notes,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            Text(
              community.notes ?? '',
              style: const TextStyle(fontSize: 14),
            ),
          ],
        ],
      ),
    );
  }

  Widget _buildInfoCard(
    BuildContext context, {
    required String title,
    required List<Widget> children,
  }) {
    return Card(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.all(16),
            child: Text(
              title,
              style: const TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          const Divider(height: 1),
          ...children,
        ],
      ),
    );
  }

  Widget _buildInfoRow(
    BuildContext context, {
    required String label,
    required String value,
    String? subtitle,
    VoidCallback? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              label,
              style: const TextStyle(
                fontSize: 14,
                color: Colors.grey,
              ),
            ),
            const SizedBox(height: 4),
            Text(
              value,
              style: const TextStyle(fontSize: 16),
            ),
            if (subtitle != null) ...[
              const SizedBox(height: 4),
              Text(
                subtitle,
                style: const TextStyle(
                  fontSize: 14,
                  color: Colors.grey,
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }

  Widget _buildSwitchRow(
    BuildContext context, {
    required String label,
    required bool value,
  }) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            label,
            style: const TextStyle(fontSize: 16),
          ),
          Icon(
            value ? Icons.check_circle : Icons.cancel,
            color: value ? Colors.green : Colors.red,
          ),
        ],
      ),
    );
  }
} 